package com.spbtce.Service.impl;

import com.spbtce.Service.SongService;
import com.spbtce.mapper.SongMapper;
import com.spbtce.model.Song;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Administrator
 * @version 1.0
 */

@Service
public class SongServiceImpl implements SongService {

    private final SongMapper songMapper;

    @Autowired
    public SongServiceImpl(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    @Override
    public void insert(Song song) {
        song.setId(uuid());

        songMapper.insert(song);
    }

    @Override
    public void deleteById(String id) {
        songMapper.deleteById(id);
    }

    @Override
    public void updateById(Song song) {
        song.setLastUpdateTime(LocalDateTime.now());

        songMapper.updateById(song);
    }

    @Override
    public Song selectById(String id) {
        return songMapper.selectById(id);
    }

    /**
     * 根据当前时间生成UUID
     *
     * @return String
     */
    private static String uuid() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.now();

        return localDate.format(formatter);
    }
}